package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.CrossZhenHuanPromotionData;
import com.yanqu.road.entity.activity.zhenhuan.ZhenHuanPromotionInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossZhenHuanPromotionImpl extends TempDataObjectDao<CrossZhenHuanPromotionData> {
    public static String getCrossZhenHuanPromotionDataSql = "select * from `t_c_zhenhuan_promotion` where `activityId`=?";
    public Map<Long, CrossZhenHuanPromotionData> getCrossZhenHuanPromotionDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossZhenHuanPromotionData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossZhenHuanPromotionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossZhenHuanPromotionData data = parseData(rs);
                dataMap.put(data.getServerId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateCrossZhenHuanPromotionDataSql = "update `t_c_zhenhuan_promotion` set promotionInfoList=?,gameServerConfirm=? where activityId=? and childType=? and serverId=?";
    @Override
    protected boolean updateData(CrossZhenHuanPromotionData crossZhenHuanPromotionData) {
        crossZhenHuanPromotionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossZhenHuanPromotionData.getPromotionInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPromotionData.getGameServerConfirm()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPromotionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPromotionData.getChildType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPromotionData.getServerId()));
        if (getDb().executeNoneQuery(updateCrossZhenHuanPromotionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossZhenHuanPromotionDataSql = "insert into `t_c_zhenhuan_promotion`(activityId,childType,serverId,promotionInfoList,gameServerConfirm) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CrossZhenHuanPromotionData crossZhenHuanPromotionData) {
        crossZhenHuanPromotionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPromotionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPromotionData.getChildType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossZhenHuanPromotionData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossZhenHuanPromotionData.getPromotionInfoList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossZhenHuanPromotionData.getGameServerConfirm()));
        if (getDb().executeNoneQuery(insertCrossZhenHuanPromotionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossZhenHuanPromotionData parseData(ResultSet rs) throws SQLException {
        CrossZhenHuanPromotionData crossZhenHuanPromotionData = new CrossZhenHuanPromotionData();
        crossZhenHuanPromotionData.setActivityId(rs.getInt("activityId"));
        crossZhenHuanPromotionData.setChildType(rs.getInt("childType"));
        crossZhenHuanPromotionData.setServerId(rs.getLong("serverId"));
        crossZhenHuanPromotionData.setPromotionInfoList(JSON.parseObject(rs.getString("promotionInfoList"), new TypeReference<List<ZhenHuanPromotionInfo>>() {}));
        crossZhenHuanPromotionData.setGameServerConfirm(rs.getInt("gameServerConfirm"));

        return crossZhenHuanPromotionData;
    }
}
